package jp.co.menox.controller.tmapp.lend;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import jp.co.menox.tmcommon.common.DataConst;
import jp.co.menox.tmcommon.model.Category;
import jp.co.menox.tmcommon.model.Lending;
import jp.co.menox.tmcommon.model.LendingHistory;
import jp.co.menox.tmcommon.model.User;
import jp.co.menox.tmserver.service.CategoryService;
import jp.co.menox.tmserver.service.LendingHistoryService;
import jp.co.menox.tmserver.service.LendingService;
import jp.co.menox.tmserver.service.UserService;

import org.slim3.controller.Controller;
import org.slim3.controller.Navigation;

public class AddTestDataController extends Controller {

    private List<Long> categoryList = new ArrayList<Long>();
    private List<Long> userList = new ArrayList<Long>();
    private List<Long> lendingList = new ArrayList<Long>();    
    
    @Override
    public Navigation run() throws Exception {
        
        addCategory("日用品");
        addCategory("キッチン");
        addCategory("スポーツ");
        addCategory("ヘルスケアー");
        addCategory("アウトドア");
        addCategory("電子機器");
        
        // User
        addUser("user1", "pass1", "ユーザー1");
        addUser("user2", "pass2", "ユーザー2");
        addUser("user3", "pass3", "ユーザー3");
        addUser("user4", "pass4", "ユーザー4");
        addUser("user5", "pass5", "ユーザー5");
        addUser("user6", "pass6", "ユーザー6");
        addUser("user7", "pass7", "ユーザー7");
        addUser("user8", "pass8", "ユーザー8");
        addUser("user9", "pass9", "ユーザー9");
        addUser("user10", "pass10", "ユーザー10");
        
        // Lending
        // user, categoryId, count, fromDate, toDate, name, comment
        addLending(userList.get(2), categoryList.get(0), 1, "2013/11/20", "2014/01/20", "自転車空気入れ", "");
        addLending(userList.get(1), categoryList.get(1), 2, "2013/11/24", "2014/02/13", "土鍋", "");
        addLending(userList.get(0), categoryList.get(1), 1, "2013/11/23", "2014/01/20", "たこ焼き器", "");
        addLending(userList.get(3), categoryList.get(1), 4, "2013/12/01", "2014/01/12", "お好み焼きヘラ", "");
        addLending(userList.get(0), categoryList.get(1), 1, "2013/11/25", "2014/01/20", "ワッフルメーカー", "");
        addLending(userList.get(1), categoryList.get(1), 2, "2013/11/27", "2014/02/05", "カセットコンロ", "");
        
        addLending(userList.get(0), categoryList.get(2), 2, "2013/11/12", "2014/02/31", "野球バット", "");
        addLending(userList.get(0), categoryList.get(2), 2, "2013/11/12", "2014/02/31", "野球グローブ", "");
        addLending(userList.get(1), categoryList.get(2), 1, "2013/11/24", "2014/02/05", "フットサルボール", "");
        addLending(userList.get(2), categoryList.get(2), 2, "2013/11/30", "2014/01/08", "卓球ラケット", "");
        addLending(userList.get(3), categoryList.get(2), 5, "2013/11/25", "2014/01/15", "バドミントンシャトル", "");
        addLending(userList.get(3), categoryList.get(2), 2, "2013/11/21", "2014/01/15", "バドミントンラケット", "");
        
        addLending(userList.get(3), categoryList.get(2), 3, "2013/12/01", "2014/01/31", "テニスボール", "");
        addLending(userList.get(0), categoryList.get(3), 1, "2013/11/28", "2014/03/31", "血圧計", "");
        addLending(userList.get(0), categoryList.get(3), 1, "2013/11/20", "2014/03/31", "体温計", "");
        addLending(userList.get(1), categoryList.get(3), 1, "2013/11/30", "2014/02/12", "歩数計", "");
        addLending(userList.get(2), categoryList.get(3), 1, "2013/11/29", "2014/01/12", "体組成計", "");
        addLending(userList.get(3), categoryList.get(4), 1, "2013/11/26", "2014/02/25", "ベンチテーブルセット", "");
        
        addLending(userList.get(3), categoryList.get(4), 1, "2013/11/27", "2014/01/25", "テント", "");
        addLending(userList.get(0), categoryList.get(4), 2, "2013/12/03", "2014/01/22", "クーラーボックス", "");
        addLending(userList.get(1), categoryList.get(4), 2, "2013/11/23", "2014/02/23", "ランタン", "");
        addLending(userList.get(2), categoryList.get(5), 1, "2013/11/24", "2014/04/01", "デジカメ", "");
        addLending(userList.get(3), categoryList.get(5), 1, "2013/11/27", "2014/05/31", "iPod nano 5", "");
        addLending(userList.get(0), categoryList.get(5), 1, "2013/11/02", "2014/02/02", "Nintendo DSi", "");
        
        // LendingHistory
        addLendingHistory(lendingList.get(8), userList.get(1), userList.get(0), DataConst.STATUS_MISYOUNIN, "2014/01/10", "2014/02/01", 1, "");
        addLendingHistory(lendingList.get(15), userList.get(1), userList.get(0), DataConst.STATUS_MISYOUNIN, "2013/12/17", "2013/12/23", 1, "");
        addLendingHistory(lendingList.get(2), userList.get(0), userList.get(3), DataConst.STATUS_MISYOUNIN, "2014/01/15", "2014/01/20", 1, "");
        addLendingHistory(lendingList.get(4), userList.get(0), userList.get(3), DataConst.STATUS_MISYOUNIN, "2014/01/08", "2014/01/11", 1, "");
        addLendingHistory(lendingList.get(23), userList.get(0), userList.get(3), DataConst.STATUS_KASHIDASHITYU, "2013/11/12", "2014/12/10", 1, "");
        addLendingHistory(lendingList.get(9), userList.get(2), userList.get(0), DataConst.STATUS_KASHIDASHITYU, "2013/12/01", "2013/12/24", 2, "");
        addLendingHistory(lendingList.get(3), userList.get(3), userList.get(0), DataConst.STATUS_KASHIDASHITYU, "2013/11/23", "2013/12/19", 2, "");
        addLendingHistory(lendingList.get(13), userList.get(0), userList.get(4), DataConst.STATUS_KASHIDASHITYU, "2013/12/02", "2013/12/6", 1, "");
        addLendingHistory(lendingList.get(20), userList.get(1), userList.get(2), DataConst.STATUS_KASHIDASHITYU, "2014/11/26", "2013/12/10", 1, "");
        addLendingHistory(lendingList.get(1), userList.get(1), userList.get(2), DataConst.STATUS_KASHIDASHITYU, "2013/11/28", "2013/12/06", 1, "");
        addLendingHistory(lendingList.get(12), userList.get(3), userList.get(1), DataConst.STATUS_KASHIDASHITYU, "2013/12/02", "2013/12/25", 1, "");
        addLendingHistory(lendingList.get(7), userList.get(0), userList.get(1), DataConst.STATUS_SYOUNINZUMI, "2013/12/07", "2013/12/10", 2, "");
        addLendingHistory(lendingList.get(0), userList.get(2), userList.get(1), DataConst.STATUS_SYOUNINZUMI, "2014/01/17", "2014/01/20", 1, "");
        addLendingHistory(lendingList.get(16), userList.get(2), userList.get(1), DataConst.STATUS_SYOUNINZUMI, "2013/12/15", "2013/12/20", 1, "");
        addLendingHistory(lendingList.get(22), userList.get(3), userList.get(1), DataConst.STATUS_SYOUNINZUMI, "2014/01/22", "2014/01/29", 1, "");
        addLendingHistory(lendingList.get(6), userList.get(0), userList.get(2), DataConst.STATUS_SYOUNINZUMI, "2014/01/10", "2014/01/15", 1, "");
        
        addLendingHistory(lendingList.get(5), userList.get(1), userList.get(0), DataConst.STATUS_SYOUNINZUMI, "2014/01/11", "2014/01/21", 2, "");
        addLendingHistory(lendingList.get(10), userList.get(3), userList.get(0), DataConst.STATUS_SYOUNINZUMI, "2013/12/25", "2014/01/10", 2, "");
       
        

        
        /*
        addLendingHistory(lendingList.get(14), userList.get(0), userList.get(5), DataConst.STATUS_SYOUNINZUMI, "2014/02/21", "2014/02/25", 1, "");
        addLendingHistory(lendingList.get(19), userList.get(0), userList.get(5), DataConst.STATUS_SYOUNINZUMI, "2014/01/20", "2014/01/27", 1, "");
        

        
        
        addLendingHistory(lendingList.get(21), userList.get(2), userList.get(3), DataConst.STATUS_SYOUNINZUMI, "2014/01/20", "2014/01/27", 1, "");
        ;
        addLendingHistory(lendingList.get(11), userList.get(3), userList.get(0), DataConst.STATUS_SYOUNINZUMI, "2013/12/25", "2014/01/10", 2, "");
        addLendingHistory(lendingList.get(17), userList.get(3), userList.get(0), DataConst.STATUS_SYOUNINZUMI, "2014/01/15", "2014/01/31", 1, "");
        addLendingHistory(lendingList.get(18), userList.get(3), userList.get(0), DataConst.STATUS_SYOUNINZUMI, "2014/01/15", "2013/01/31", 1, "");
        */
        
        return null;
    }
    
    private void addCategory(String name) {
        Category category = new Category();
        category.setName(name);
        
        category = new CategoryService().insert(category);
        categoryList.add(category.getId());
    }
    
    private void addUser(String loginId, String password, String name) {
        User user = new User();
        user.setLoginId(loginId);
        user.setPassword(password);
        user.setName(name);
        
        user = new UserService().insert(user);
        userList.add(user.getId());
    }
    
    @SuppressWarnings("deprecation")
    private void addLending(long userId, long categoryId, int count, 
            String fromDate, String toDate, String name, String comment) {
        Lending lending = new Lending();
        lending.setUserId(userId);
        lending.setCategoryId(categoryId);
        lending.setCount(count);
        lending.setFromDate(new Date(fromDate));
        lending.setToDate(new Date(toDate));
        lending.setName(name);
        lending.setComment(comment);
        
        lending = new LendingService().insert(lending);
        lendingList.add(lending.getId());
    }
    
    @SuppressWarnings("deprecation")
    private void addLendingHistory(long lendingId, long lendingUser, long borrowingUser, 
            int statusType, String fromDate, String returnPlanDate, int count, String sharingId) {
        LendingHistory lendingHistory = new LendingHistory();
        lendingHistory.setLendingId(lendingId);
        lendingHistory.setLendingUser(lendingUser);
        lendingHistory.setBorrowingUser(borrowingUser);
        lendingHistory.setStatusType(statusType);
        lendingHistory.setLentDate(new Date(fromDate));
        lendingHistory.setFromDate(new Date(fromDate));
        lendingHistory.setToDate(new Date(returnPlanDate));
        lendingHistory.setReturnPlanDate(new Date(returnPlanDate));
        lendingHistory.setCount(count);
        lendingHistory.setSharingId(sharingId);
        
        new LendingHistoryService().insert(lendingHistory);
    }
}